<template>
	<div>
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="900px">
			<div class="layout-padding-auto layout-scroll-padding">
				<div class="search mb15">
					<el-row :gutter="10">
						<el-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6">
							<el-form-item label="模版名称:">
								<el-input v-model="state.search.name" size="default" clearable placeholder="请输入活动名称" style="width: 100%"> </el-input>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" v-show="state.isToggle">
							<el-form-item label="创建时间:">
								<el-date-picker
									type="datetimerange"
									v-model="state.search.searchDateTime"
									start-placeholder="创建开始时间"
									end-placeholder="创建时间"
									style="width: 100%"
								/>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
							<el-form-item class="table-form-btn" label-width="10px">
								<div class="table-form-btn-toggle ml10 cursor-pointer" @click="state.isToggle = !state.isToggle">
									<span>{{ state.isToggle ? '收筛选' : '展筛选' }}</span>
									<SvgIcon :name="state.isToggle ? 'ele-ArrowUp' : 'ele-ArrowDown'" />
								</div>
								<el-button size="default" type="primary" class="ml10" @click="getTableData">
									<el-icon>
										<ele-Search />
									</el-icon>
									查询
								</el-button>
								<el-button size="default" type="primary" class="ml10" @click="onOpen('add', '')">
									<el-icon>
										<ele-FolderAdd />
									</el-icon>
									新增模版
								</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
				<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%" @expand-change="dataChange" row-key="id">
					<el-table-column type="expand">
						<template #default="props">
							<div class="rate-area-title"><el-text tag="i" size="lager">配送区域</el-text></div>
							<el-table :data="props.row.areaRates">
								<el-table-column label="可配送区域" prop="name">
									<template #default="scope">
										<span v-for="(item, index) in scope.row.areas" :key="index"> {{ item.name }},</span>
									</template>
								</el-table-column>
								<el-table-column :label="props.row.calType == 0 ? '首件' : '首重(kg)'" prop="firstCount"> </el-table-column>
								<el-table-column label="运费(元)" prop="firstRate" />
								<el-table-column label="续件" prop="secondCount" />
								<el-table-column label="续费(元)" prop="secondRate" />
							</el-table>
						</template>
					</el-table-column>
					<el-table-column prop="dispatchName" label="模版名称" show-overflow-tooltip></el-table-column>
					<el-table-column prop="calType" label="计费类型" show-overflow-tooltip>
						<template #default="scope">
							<el-tag v-if="scope.row.calType == 0">按件数</el-tag>
							<el-tag v-else-if="scope.row.calType == 1">按重量</el-tag>
						</template>
					</el-table-column>
					<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
					<el-table-column prop="updateTime" label="修改时间" show-overflow-tooltip></el-table-column>
					<el-table-column prop="updateUsername" label="操作人" show-overflow-tooltip></el-table-column>
					<el-table-column label="操作" width="200">
						<template #default="scope">
							<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onChoice(scope.row)"
								>选择</el-button
							>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				:page-sizes="[10, 15, 20, 30, 50, 100]"
				v-model:current-page="state.tableData.paging.currentPage"
				background
				v-model:page-size="state.tableData.paging.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.paging.totalCount"
			>
			</el-pagination>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';

import { dispatchApi } from '/@/api/shop/dispatch/index';
import messageUtils from '/@/utils/messageUtils';
import { resolveEndTime, resolveStartTime } from '/@/utils/time/utils';

const emit = defineEmits(['choice']);
const state = reactive({
	isToggle: false,
	search: {
		name: '',
		searchDateTime: '',
	},
	tableData: {
		data: [],
		total: 0,
		loading: false,
		paging: {
			currentPage: 1,
			pageSize: 15,
			totalCount: 0,
			totalPages: 0,
		},
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

const onCancel = () => {
	closeDialog();
};

const closeDialog = () => {
	console.log('关闭窗口');
	state.dialog.isShowDialog = false;
};

const onChoice = (row: any) => {
	emit('choice', '');
	onCancel();
};

const onOpen = () => {
	state.dialog.isShowDialog = true;
	getTableData();
};

const dataChange = async (row: any, expandRows: any[]) => {
	console.log('row:', row, ' ,expandRows: ', expandRows);
	const { data } = await dispatchApi().rateAares(row.id);
	console.log('data: ', data);
	row.areaRates = data;
};

// 分页改变
const onHandleSizeChange = (pageSize: number) => {
	state.tableData.paging.pageSize = pageSize;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (currentPage: number) => {
	state.tableData.paging.currentPage = currentPage;
	getTableData();
};

const getTableData = async () => {
	try {
		state.tableData.loading = true;
		const { data } = await dispatchApi().search({
			dispatchName: state.search.name,
			startTime: resolveStartTime(state.search.searchDateTime),
			endTime: resolveEndTime(state.search.searchDateTime),
			offset: (state.tableData.paging.currentPage - 1) * state.tableData.paging.pageSize,
			limit: state.tableData.paging.pageSize,
		});
		state.tableData.data = data.datas;
		state.tableData.paging.currentPage = data.paging.currentPage;
		state.tableData.paging.totalCount = data.paging.totalCount;
		console.log('data: ', data);
	} catch (ex) {
		messageUtils().error('加载失败请重试');
	} finally {
		state.tableData.loading = false;
	}
};
</script>

<style scoped lang="scss">
.rate-area-title {
	// margin-left: 16px;
	padding-left: 16px;
	width: 100%;
	background-color: rgba(236, 240, 241, 1);
	height: 30px;
	line-height: 30px;
}
</style>
